#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
SOAP アプリケーション機能

Mikan Python Web Framework
"""

__author__ = "K.Sonohara"
__status__ = "develop"
__version__ = "0.0.0_0"
__date__    = "2012/12/01"
__license__ = 'LGPL'

# ------------------------------------------------------------
## インポート
# ------------------------------------------------------------
# Python

# ESiPC
from esipc import ESiPCProperty

# Mikan
from mikan.app import MikanAuthApp

# ------------------------------------------------------------
## 変数
# ------------------------------------------------------------

# ------------------------------------------------------------
## 関数
# ------------------------------------------------------------

# ------------------------------------------------------------
## クラス
# ------------------------------------------------------------

# ------------------------------------------------------------
## Mikan SOAP アプリケーションクラス
#
class MikanSOAPApp(MikanAuthApp):
	# ----------------------------------------------------
	## パーフォームクラス フィールドゲッター
	def get_soap_dispatcher(self):
		return self.FSOAPDispatcher

	def __init__(self, request, dispatcher):
		super(MikanSOAPApp, self).__init__(request)

		self.FSOAPDispatcher = dispatcher

	def perform(self):
		if self.request.isgetmethod:
			return self.perform_get()
		elif self.request.ispostmethod:
			return self.perform_post()
		return False

	def perform_get(self):
#		path = self.request.environ.get('PATH_INFO').lstrip('/')
#		query = self.request.environ.get('QUERY_STRING')

#		if path != "" and path not in self.getSOAPDispatcher().methods.keys():
		content = self.soapdispatcher.wsdl()
#		else:
#			req, res, doc = self.getSOAPDispatcher().help(path)
#			if len(query) == 0 or query=="request":
#				content = req
#			else:
#				content = res

		self.request.contentType = 'text/xml'
		self.request.content = content

		return True

	def perform_post(self):
		length = int(self.request.environ['CONTENT_LENGTH'])
		request = self.request.environ['wsgi.input'].read(length)
		response = self.soapdispatcher.dispatch(request)

		self.request.contenttype = 'text/xml'
		self.request.content = response

		return True

	# ----------------------------------------------------
	## Property
	soapdispatcher = ESiPCProperty(get_soap_dispatcher, None, None)

# ------------------------------------------------------------
## メイン関数
# ------------------------------------------------------------
if __name__ == '__main__':
	pass

# ------------------------------------------------------------
